<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Pair extends CI_Controller {

	public $send_sms = false;

	function __construct() {
		parent::__construct();
		$this -> load -> library("login_lib");
		$this -> load -> library("pair_lib");
		$this -> load -> library('pattern_lib');
		$this -> load -> library('first_lib');
		$this -> load -> library('show_test');
		$this -> load -> library("send_html");
		$this -> load -> library('show_test');
		$this -> load -> library("mapping");
		$this -> load -> library('pagination');	
		$this -> load -> library('base_data_lib');	
		
		
		$this -> load -> model('object_temporary_model');		
				
		$this -> load -> model('pair_model');			
		$this -> load -> model('client/contact','contact');	
		$this -> load -> model('windows_model');			
		$this -> load -> model('market_state_model');			
		$this -> load -> model('parking_type_model');
		$this -> load -> model('pattern_model');		
		$this -> load -> model('parking_type_model');
		$this -> load -> model('object_model');
		$this -> load -> model('demand_model');
		$this -> load -> model('client_model');
		$this -> load -> model("demand_m");
		$this -> load -> model("object_m");
		$this -> load -> model('users_model');		
		$this -> load -> model('parking_type_model');
		$this -> load -> model('object_state_model');
		$this -> load -> model('market_state_model');
		$this -> load -> model('building_name_model');
		$this -> load -> model('currency_model');
		$this -> load -> model('area_unit_model');
		$this -> load -> model('pair_model');
		$this -> load -> model('market_model');
		$this -> load -> model('client_model');
		$this -> load -> model('users_model');
		$this -> load -> model('object_state_model');
		$this -> load -> model('market_state_model');
		$this -> load -> model('building_name_model');
		$this -> load -> model('building_name_model');
		$this -> load -> model('emphasis_area_model');
		$this -> load -> model('address_city_model');
		$this -> load -> model('address_county_model');
		$this -> load -> model('building_name_model');
		$this -> load -> model('emphasis_area_model');
		$this -> load -> model('address_city_model');
		$this -> load -> model('address_county_model');
	}

	public function y_show() {

		$CI = &get_instance();
		echo $CI -> login_lib -> users_id();
		echo '/';
		echo $CI -> login_lib -> users_name();
		//print_r($user_purview);
	}

	public function update_excerpt_sql() {

		$id = $this -> input -> post("id");
		$_sql = $this -> input -> post("sql");

		//echo $id.'// -  -';
		//echo $_sql;
		$_data = array();
		$_data = array('excerpt_sql' => $_sql);
		
		$this -> pair_model -> update_excerpt_sql($id, $_data);
	}

	public function verfiy_show() {

		$vr_row = $this -> pair_model -> last_verify();
		$row = $vr_row -> row();
		return $row -> code;
	}

	public function demand() {

		$id = $this -> input -> get('id');
		if (empty($id))
			$id = 0;

		$page = $this -> input -> get('page');
		if (empty($page))
			$page = 0;

		$per_page = $this -> input -> get('per_page');
		if (empty($per_page))
			$per_page = 0;

		$html = $this -> init("單筆需求配對", "jquery", "table");
		

		$demand_res = $this -> demand_model -> get_one_demand_info($id);

		$html["demand_info"] = $demand_res;
		$html["demand_fields"] = $this -> demand_m -> get_fields();
		$demand_fields = $this -> object_m -> get_fields();
		//
		
		$html["fucking_show_demand"] = $this -> mapping -> demand($demand_res, $html["demand_fields"]);

		$html["demand_res"] = $demand_res;
		
		$html["parking_type"] = $this -> parking_type_model -> get_all();
		$demand = $demand_res -> row();

		$where_kaki = array();
		$debug_flag = true;

		if ($debug_flag)
			foreach ($demand as $row => $_val) {
				if (!empty($_val)) {
					if (is_numeric($_val)) {
						if ((int)$_val != 0) {
							$where_kaki[$row] = $_val;
						}
					} else {
						$where_kaki[$row] = $_val;
					}
				}
			}

		$where = array();
		/*         * *******對的*********** */
		
		$html["query_condition"] = '';
		//$condition;

		if (!empty($where_kaki)) {
			$row_nums = $this -> object_m -> match_object_rows($where_kaki);
		} else {
			$row_nums = '';
		}

		$config['base_url'] = base_url() . "pair/demand?id=" . $id . "&page=" . $page;
		$config['first_link'] = "首頁";
		$config['last_link'] = "尾頁";

		if ($row_nums) {
			$config['total_rows'] = 10;
			//$row_nums -> num_rows;
		}
		$config['per_page'] = 15;

		$per_page;
		$config['page_query_string'] = TRUE;
		$this -> pagination -> initialize($config);
		$limit = $per_page . " , " . $config['per_page'];

		/*********對的************/

		$html["victor_page"] = $this -> pagination -> create_links();

		//******************************************************************//
		//配對
		//demand_pair_object_sql($data , $_start_num , $load_count)
		$load_count=10;

		$page;
		if(empty($_GET["page"]) ){
			$page=0;
		}else{
			$_page= $_GET["page"];
			$page= ($_GET["page"]*$load_count)+1; 
		}

		$_start_num=$page; 

		$sql_schema = $this -> first_lib -> demand_pair_object_sql($where_kaki, $_start_num , $load_count);

		//$_result = $this -> pattern_model -> just_do_query($sql_schema);
		$html["object_state"] = $this -> object_state_model   -> get_object_state();		
		$html["market_state"] = $this -> market_state_model   -> get_market_state();		
		$html["building_name"] = $this -> building_name_model -> get_building_name();		
		$html["parking_type"] = $this -> parking_type_model   -> get_all();
		
		
		$_data=$html;
		$_data = $this->base_data_lib->collcet_object_market_ajax();
		$_data["object_state"]  = $this -> object_state_model   -> get_object_state();		
		//$_data["users"]  = $this -> object_state_model   -> get_object_state();
		$_data["market_state"]  = $this -> market_state_model   -> get_market_state();		
		$_data["building_name"] = $this -> building_name_model  -> get_building_name();		
		$_data["parking_type"]  = $this -> base_data_lib   -> get_parking_type_array();
			
		$object_modal =array();
		$market_modal =array();	
			
		
		foreach($sql_schema as $key => $_sql){
					
			$_result = $this -> pattern_model -> just_do_query($_sql);
			//echo '[';
			$cc["page_count"] =$_result->num_rows;
			//echo ']';
			$html['result'] = $_result;
			if($key==0){
				
				foreach($html['result']->result() as $row){
										
					$_data["object"]=$row;

					$_data["client"]=$this->client_model->get_client_info($row->client_id);
					$_data["contact"]=$this->contact->get_contact_by_id($row->client_contact_id);

					$object_modal[]=$this -> load -> view('demand/object_info' , $_data  , true);
					$this -> object_model -> update_object_point_count($row -> object_id);
				}
				
				
				$container[1]=$this -> load -> view('demand/demand_pair_ajax' , $html  , true);


			}elseif($key==1){
				
				
				foreach($html['result']->result() as $row){
										
					$_data["object"]=$row;
					$_data["client"]=$this->client_model->get_client_info($row->client_id);
					$_data["contact"]=$this->contact->get_contact_by_id($row->client_contact_id);
					$market_modal[]=$this -> load -> view('demand/market_info' , $_data  , true);
				}				
				
				$container[2]=$this -> load -> view('demand/market_pair_ajax' , $html  , true);
			}
			
		}
		
		$html["market_modal"]=$market_modal;
		$html["object_modal"]=$object_modal;

			$cc["page"]=!empty($_page)?$_page:'';
			$cc["_start_num"]=!empty($_start_num)?$_start_num:'';
			$cc["_load_count"]=!empty($load_count)?$load_count:'';
			$cc["_id"]=!empty($_GET["id"])?$_GET["id"]:'';

		$container[0]=$this -> load -> view('demand/demand_pair_ajax_head' ,$cc  , true);		
		$container[3]='</table>';//3－＞table 的end
		$html["containers"]=$container;
		
		
		$send_html_header = '<table border=2 align=center width=70% cellspacing=0 cellpadding=0>';
		$this -> load -> library("send_html");

		$send_html;

		$demand_html = $this -> send_html -> de_html($demand);
		$send_html = $send_html_header . $demand_html;

		
		$temp = '';
		$this -> demand_model -> update_demand_point_count($id);

		if ($_result -> num_rows > 0) {

			foreach ($_result->result() as $obj_row) {
				//print_r($obj_row);
				
				//echo '<br>';
				//echo '<br>';
				//echo '<br>';
				//echo '<br>';
				/*
				$this -> object_model -> update_object_point_count($obj_row -> object_id);

				$client_query = '';
				if (!empty($obj_row -> broker)) {

					$client_query = $this -> users_model -> get_user_by_user($obj_row -> broker);
					$url = '';
					if ($client_query -> num_rows >= 1) {

						$url = "?object_id=" . $obj_row -> object_id . "&demand_id=" . $demand -> demand_id;
						$this -> pair_model -> insert_verify($url);
						$object_html = '';

						$client_if = $client_query -> row();

						$ob_html = $this -> send_html -> ob_html($obj_row);

						$object_html .= $ob_html;
						$object_html .= '</table>';

						$this -> send_mails($client_if -> email, '需求配物件', $client_if -> name, '需求配物件', $send_html . $object_html);

						$verify_code = $this -> verfiy_show();
						$sms_content = $this -> send_html -> dem_obj_sms_body($obj_row, $demand, $verify_code);

						$this -> send_sms($client_if -> phone, $client_if -> name, $sms_content);
					}
				}
				
				*/
			}

		} else {
			/*
			$sms_content = $this -> send_html -> demand_sms_no_pair($demand);
			$send_html .= '</table>沒有配對到';
			$all_users = $this -> get_all_users();
			$group_mail = '';

			foreach ($all_users as $user) {

				if (!empty($user -> phone) AND !empty($user -> name)) {
					$this -> send_sms($user -> phone, $user -> name, $sms_content);
				}

				if (!empty($user -> email))
					$this -> send_mails($user -> email, '沒有配對到！！', $user -> name, '需求配物件', $sms_content);

			}
			*/
		}

		if (empty($demand -> demand_id)) {
			$html["result"] = '';
		}

		//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//
		//show-box - data
		
		$html["building_name"]  = $this -> building_name_model -> get_all();
		$html["emphasis_area"]  = $this -> emphasis_area_model -> get_all();
		$html["address_city"]   = $this -> address_city_model -> get_all();		
		$html["address_county"] = $this -> address_county_model -> get_by_id('1');

		//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//

		$this -> load -> view('demand/demand_pair', $html);

	}


	public function market() {
		
		$html = $this -> init("市調配對物件", "jquery", "table");
		$id=$this->input->get("id");
		//查詢市調
		$market = $this->market_model->get($id);
		//市調內容
		$html["market"] = $market;
		$market_info=$market->row();
		foreach ($market_info as $row => $_val) {

			if (!empty($_val)) {
				if (is_numeric($_val)) {
					if ((int)$_val != 0) {

						$where_kaki[$row] = $_val;
					}
				} else {
					$where_kaki[$row] = $_val;
				}
			}
		}
		

		$load_count=10;

		$page;
		$_page;
		if(empty($_GET["page"]) ){

			$page=0;
			$_page=0;

		}else{

			$_page= $_GET["page"];
			$page= ($_GET["page"]*$load_count)+1; 

		}

		$_start_num=$page;
		//object
		//配對－＞配需求
		//echo $_start_num .'/,/'. $load_count;
		$sql_schema = $this -> first_lib -> object_pair_demand_sql($where_kaki , $_start_num , $load_count);
		
		//配對－＞配需求
		//$sql_schema = $this -> first_lib -> object_pair_demand_sql($where_kaki);
		//配對結果
		$_result = $this -> pattern_model -> just_do_query($sql_schema);
		//echo $sql_schema;
		$html['result']=$_result;
		
		
		
		
		$html["building_name"] = $this -> building_name_model -> get_all_new();		
		$html["emphasis_area"] = $this -> emphasis_area_model -> get_all_new();
		$html["address_city"] 	= $this -> address_city_model -> get_all_new();
		$html["address_county"] 	= $this -> address_county_model -> get_by_id('1');
		$html["users"] 			= $this -> base_data_lib->get_all_users_array();
		$html["object_usages"] 	= $this ->base_data_lib ->get_object_usage_array();

		$html["currency"] 	= $this ->base_data_lib ->get_currency_array();
		$html["area_unit"] 	= $this ->base_data_lib ->get_area_unit_array();

		$html["required_level"] 	= $this ->base_data_lib ->get_required_level_model_array();
		
		
		$_data=$html;
		$demand_modal=array();
		foreach($_result->result() as $row){
			//echo $row->demand_id;		
			//echo '<br>';			
			$_data["demand"]=$row;

			$_data["client"]=$this->client_model->get_client_info($row->client_id);
			$_data["contact"]=$this->contact->get_contact_by_id($row->client_contact_id);

			$demand_modal[]=$this -> load -> view('object/demand_info' , $_data  , true);
			
		}
			
		//跳窗
			
		 if(count($demand_modal)>=1){
		 	foreach ($demand_modal as $key => $value) {
				 echo $value;
			 }
		 }
			
		//$html["show_box_infos"] = (count($demand_modal)>1 ) ? $demand_modal : '';
		
		
		$html["_id"] = $id;	

		$html["page"] = $_page;				
		$html["result_page"] = $this->load->view('market/market_pair_ajax' , $html , true );
		//die();		
		$this -> load -> view('market/market_pair', $html);		
		
	}	

	public function pair_object_ajax() {

		/////////////////////////////////////////////////
		//ref data
		/////////////////////////////////////////////////

		

		$html["area_unit"] = $this -> area_unit_model -> get_all();
		$html["currencies"] = $this -> currency_model -> get_all();
		$html["parking_type"] = $this -> parking_type_model -> get_all();
		$html["object_state"] = $this -> object_state_model -> get_object_state();
		$html["market_state"] = $this -> market_state_model -> get_market_state();
		$html["building_name"] = $this -> building_name_model -> get_building_name();

		//////////////////////////////////////////////

		$this -> load -> model('whr/excerpt', 'excerpt');
		$verify_code = $this -> input -> post('verify_code');
		$h_excerpt_sql_id = $this -> input -> post('exc_id');
		$flg = $this -> input -> post('flg');

		$data = array('flag' => $flg);
		$this -> excerpt -> update($h_excerpt_sql_id, $data);
		$this -> load -> model("demand_m");

		$re = $this -> demand_m -> excerpt_demand($verify_code);
		$re_field = $this -> demand_m -> excerpt_demand_field($verify_code);

		$html["resu"] = $re;
		//結果
		$html["resu_fields"] = $re_field;
		//欄位

		$demand_id_s = array();
		foreach ($re->result() as $row) {
			$demand_id_s[] = $row -> demand_id;
		}

		$html["display_demand"] = $this -> demand_m -> show_display_demand($demand_id_s);

		$this -> load -> view('object/object_pair_ajax', $html);
	}

	public function pair_demand_ajax() {
		/////////////////////////////////////////////////
		//ref data
		/////////////////////////////////////////////////
		$this -> load -> model('object_m');
		$this -> load -> model('parking_type_model');
		$html["parking_type"] = $this -> parking_type_model -> get_all();
		$this -> load -> model('object_state_model');
		$html["object_state"] = $this -> object_state_model -> get_object_state();
		$this -> load -> model('market_state_model');
		$html["market_state"] = $this -> market_state_model -> get_market_state();
		$this -> load -> model('building_name_model');
		$html["building_name"] = $this -> building_name_model -> get_building_name();
		//////////////////////////////////////////////

		$this -> load -> model('whr/excerpt', 'excerpt');
		$verify_code = $this -> input -> post('verify_code');
		$h_excerpt_sql_id = $this -> input -> post('exc_id');
		$flg = $this -> input -> post('flg');

		//print_r($_POST);
		$data = array('flag' => $flg);
		$this -> excerpt -> update($h_excerpt_sql_id, $data);
		$re = $this -> query_deman_ajax($verify_code);
		$re_fields = $this -> object_m -> excerpt_object_field($verify_code);
		$html["result"] = $re;
		$html["result_fields"] = $re_fields;

		// 查詢
		$object_id_s = array();
		foreach ($re->result() as $row) {
			$object_id_s[] = $row -> object_id;
		}

		$html["display_object"] = $this -> object_m -> display_object($object_id_s);

		$this -> load -> view('demand/demand_pair_ajax', $html);
	}

	public function query_deman_ajax($verify_code) {

		$this -> load -> model('object_m');
		$query = $this -> object_m -> excerpt_object($verify_code);
		return $query;

	}

	public function insert_sms($to, $name, $content) {

		$this -> load -> model('sms_model');
		$data['to'] = $to;
		$data['name'] = $name;
		$data['content'] = $content;
		$data['status'] = 'N';
		$this -> sms_model -> add($data);

	}

	public function send_mails($mailaddr, $title, $rr, $name, $content) {
		/*
		 $this -> load -> library('mail/phpmailer');

		 $mail = new PHPMailer();

		 $is_gmail = false;

		 if ($is_gmail) {

		 $mail -> Debugoutput = 'html';
		 //Set the hostname of the mail server
		 $mail -> Host = 'smtp.gmail.com';
		 //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
		 $mail -> Port = 465;
		 //Set the encryption system to use - ssl (deprecated) or tls
		 $mail -> SMTPSecure = 'ssl';
		 //Whether to use SMTP authentication
		 $mail -> SMTPAuth = true;
		 $mail -> CharSet = "utf-8";
		 //郵件編碼
		 //Username to use for SMTP authentication - use full email address for gmail
		 $mail -> Username = "withvictor@gmail.com";
		 //Password to use for SMTP authentication
		 $mail -> Password = "2uuiajgialmy0417";
		 //Set who the message is to be sent from
		 $mail -> setFrom('kaki@kakichung.com', 'First Last');
		 //Set an alternative reply-to address
		 //$mail->addReplyTo($mailaddr, $name);
		 //Set who the message is to be sent to
		 $mail -> addAddress($mailaddr, $name);
		 //Set the subject line
		 $mail -> Subject = $title;
		 //Read an HTML message body from an external file, convert referenced images to embedded,
		 //convert HTML into a basic plain-text alternative body
		 $mail -> msgHTML($content);
		 //Replace the plain text body with one created manually
		 //$mail->AltBody = 'This is a plain-text message body';

		 $mail -> Send();
		 $mail -> ClearAddresses();
		 } else {
		 // Set PHPMailer to use the sendmail transport
		 $mail -> isSendmail();
		 $mail -> CharSet = "utf-8";
		 //郵件編碼
		 //Set who the message is to be sent from
		 $mail -> setFrom('from@example.com', 'First Last');
		 //Set an alternative reply-to address
		 $mail -> addReplyTo($mailaddr, 'First Last');
		 //Set who the message is to be sent to
		 $mail -> addAddress($mailaddr, 'John Doe');
		 //Set the subject line
		 $mail -> Subject = $title;
		 //Read an HTML message body from an external file, convert referenced images to embedded,
		 //convert HTML into a basic plain-text alternative body
		 $mail -> msgHTML($content);
		 //Replace the plain text body with one created manually
		 $mail -> AltBody = 'This is a plain-text message body';
		 $mail -> Send();
		 }
		 */
	}

	public function get_all_users() {

		$this -> load -> model('users_model', 'm_user');
		$d_user = $this -> m_user -> get_all();
		return $d_user;
	}

	public function demand_ajax($id = "") {
		$this -> load -> model('object_model');
		$config['per_page'] = 15;
		$query_condition = $this -> input -> post('query_condition');
		$page = $this -> input -> post('page');
		$sequence = $this -> input -> post('sequence');
		$where = $this -> resolve($query_condition, "where");
		$p = $config['per_page'] * ($page - 1);
		$limit = $config['per_page'] . "," . $p;
		$html["result"] = $this -> object_model -> object_pair($where, $limit);
		$this -> load -> model('object_state_model');
		$html["object_state"] = $this -> object_state_model -> get_object_state();
		$this -> load -> model('market_state_model');
		$html["market_state"] = $this -> market_state_model -> get_market_state();
		$this -> load -> model('building_name_model');
		$html["building_name"] = $this -> building_name_model -> get_building_name();
		$this -> load -> view('demand_pair_ajax', $html);
	}

	public function demand_analog_pairing($id = "") {

		$html = $this -> init("模擬需求配對", "jquery", "table");
		$html['demand_temporary_id'] = $id;
		$this -> load -> model('demand_temporary_model');
		$demand = $this -> demand_temporary_model -> get_demand_info($id);
		$where = array();
		$w = array();
		if ($demand -> type_sell == 1 || $demand -> type_consignment == 1)
			$where['type_sale'] = 1;
		if ($demand -> type_rent == 1)
			$where['type_rent'] = 1;
		if ($demand -> object_usage != "") {
			$where['object_usage'] = $demand -> object_usage;
		}

		if ($demand -> building_area1 != "") {
			$where['building_area >='] = $demand -> building_area1;
		}
		if ($demand -> building_area2 != "") {
			$where['building_area <='] = $demand -> building_area2;
		}

		if ($demand -> parking_number != "") {

			if ($demand -> type_sell == 1 || $demand -> type_consignment == 1)
				$where['sale_parking >='] = $demand -> parking_number;
			if ($demand -> type_rent == 1)
				$where['rent_parking >='] = $demand -> parking_number;

		}
		if ($demand -> indoor_area1 != "") {
			$where['main_building_area >='] = $demand -> indoor_area1;
		}
		if ($demand -> indoor_area2 != "") {
			$where['main_building_area <='] = $demand -> indoor_area2;
		}
		if ($demand -> land_area1 != "") {
			$where['stakeholders_land_area >='] = $demand -> land_area1;
		}
		if ($demand -> land_area2 != "") {
			$where['stakeholders_land_area <='] = $demand -> land_area2;
		}
		if ($demand -> specify_building1 != "" && $demand -> specify_building1 != 0) {
			$where['main_building_area >='] = $demand -> specify_building2;
		}
		if ($demand -> specify_building2 != "" && $demand -> specify_building2 != 0) {
			$where['main_building_area <='] = $demand -> specify_building2;
		}
		if ($demand -> key_areas1 != "") {
			$w['key_areas'][1] = $demand -> key_areas1;
			//area
		}
		if ($demand -> key_areas2 != "") {
			$w['key_areas'][2] = $demand -> key_areas1;
			//area
		}
		if ($demand -> sections1 != "") {
			$w['sections1'] = $demand -> sections1;
		}
		if ($demand -> sections2 != "") {
			$w['sections2'] = $demand -> sections2;
		}
		if ($demand -> object_name1 != "") {
			$w['object_name'][1] = $demand -> object_name1;
		}
		if ($demand -> object_name2 != "") {
			$w['object_name'][2] = $demand -> object_name2;
		}

		if ($demand -> mrt_line1 != "") {
			$where['mrt_line1'] = $demand -> mrt_line1;
		}

		if ($demand -> mrt_start1 != "" && $demand -> mrt_start1 != 0) {
			//$where['mrt1'] = $demand->mrt_start1;
		}

		if ($demand -> mrt_until1 != "" && $demand -> mrt_until1 != 0) {
			//$where['mrt1'] = $demand->mrt_until1;
		}

		if ($demand -> mrt_line2 != "" && $demand -> mrt_line2 != 0) {
			$where['mrt_line2'] = $demand -> mrt_line2;
		}

		if ($demand -> mrt_start2 != "" && $demand -> mrt_start2 != 0) {
			//$where['mrt2'] = $demand->mrt_start2;
		}

		if ($demand -> mrt_until2 != "" && $demand -> mrt_until2 != 0) {
			//$where['mrt2'] = $demand->mrt_until2;
		}

		if ($demand -> range_east != "" && $demand -> range_east != 0) {
			//$where['location_map1'] = $demand->range_east;
		}
		if ($demand -> range_north != "" && $demand -> range_north != 0) {
			//$where['location_map1'] = $demand->range_north;
		}
		if ($demand -> range_west != "" && $demand -> range_west != 0) {
			//$where['location_map2'] = $demand->range_west;
		}
		if ($demand -> range_south != "" && $demand -> range_south != 0) {
			//$where['location_map2'] = $demand->range_south;
		}
		if ($demand -> park1 != "" && $demand -> park1 != 0) {
			$where['park1'] = $demand -> park1;
		}
		if ($demand -> park2 != "" && $demand -> park2 != 0) {
			$where['park2'] = $demand -> park2;
		}
		if ($demand -> school1 != "" && $demand -> school1 != 0) {
			$where['school_district_primary'] = $demand -> school1;
		}
		if ($demand -> school2 != "" && $demand -> school2 != 0) {
			$where['school_district_high'] = $demand -> school2;
		}
		if ($demand -> administrative_area1 != "" && $demand -> administrative_area1 != 0) {
			$w['administrative_area'][1] = $demand -> administrative_area1;
		}
		if ($demand -> administrative_area2 != "" && $demand -> administrative_area2 != 0) {
			$w['administrative_area'][2] = $demand -> administrative_area2;
		}

		if ($demand -> reported_rate1 != "" && $demand -> reported_rate1 != 0) {
			$where['reported_rate >='] = $demand -> reported_rate1;
		}
		if ($demand -> reported_rate2 != "") {
			$where['reported_rate <='] = $demand -> reported_rate2;
		}
		if ($demand -> year_of_construction != "" && $demand -> year_of_construction != 0) {
			$where['year_of_construction'] = $demand -> year_of_construction;
		}
		if ($demand -> object_case_type != "" && $demand -> object_case_type != 0) {
			//$where['object_case_type'] = $demand->object_case_type;
		}
		if ($demand -> year_of_construction != "" && $demand -> year_of_construction != 0) {
			$where['year_of_construction'] = $demand -> year_of_construction;
		}
		if ($demand -> windows != "" && $demand -> windows != 0) {
			$where['windows'] = $demand -> windows;
		}
		if ($demand -> facing_the_household != "" && $demand -> facing_the_household != 0) {
			$where['household_door'] = $demand -> facing_the_household;
		}
		if ($demand -> building_facing != "" && $demand -> building_facing != 0) {
			$where['building_entrance'] = $demand -> building_facing;
		}
		if ($demand -> designated_floors1 != "" && $demand -> designated_floors1 != 0) {
			//$w['floor'] = $demand->designated_floors1;
		}

		if ($demand -> object_styles != "" && $demand -> object_styles != 0) {
			//where['windows'] = $demand->object_styles;
		}
		if ($demand -> public_facilities != "" && $demand -> public_facilities != 0) {
			$where['public_facilities'] = $demand -> public_facilities;
		}

		if ($demand -> accessories_with != "" && $demand -> accessories_with != 0) {
			$where['rent_ancillary'] = $demand -> accessories_with;
		}
		if ($demand -> management != "" && $demand -> management != 0) {
			$where['management'] = $demand -> management;
		}
		if ($demand -> number_of_rooms1 != "" && $demand -> number_of_rooms1 != 0) {
			$where['indoor_pattern_house >='] = $demand -> number_of_rooms1;
		}
		if ($demand -> number_of_rooms2 != "" && $demand -> number_of_rooms2 != 0) {
			$where['indoor_pattern_house <='] = $demand -> number_of_rooms2;
		}

		if ($demand -> number_of_hall1 != "" && $demand -> number_of_hall1 != 0) {
			$where['indoor_pattern_hall >='] = $demand -> number_of_hall1;
		}
		if ($demand -> number_of_hall2 != "" && $demand -> number_of_hall2 != 0) {
			$where['indoor_pattern_hall <='] = $demand -> number_of_hall2;
		}

		if ($demand -> number_of_toilet1 != "" && $demand -> number_of_toilet1 != 0) {
			$where['indoor_pattern_guard >='] = $demand -> number_of_toilet1;
		}
		if ($demand -> number_of_toilet2 != "" && $demand -> number_of_toilet2 != 0) {
			$where['indoor_pattern_guard <='] = $demand -> number_of_toilet2;
		}

		if ($demand -> number_of_suite1 != "" && $demand -> number_of_suite1 != 0) {
			$where['indoor_pattern_suite >='] = $demand -> number_of_suite1;
		}
		if ($demand -> number_of_suite2 != "" && $demand -> number_of_suite2 != 0) {
			$where['indoor_pattern_suite <='] = $demand -> number_of_suite2;
		}
		if ($demand -> partition_the != "" && $demand -> partition_the != 0) {
			$where['land_use_zoning'] = $demand -> partition_the;
		}
		if ($demand -> floor_height != "" && $demand -> floor_height != 0) {
			$where['floor_height'] = $demand -> floor_height;
		}
		if ($demand -> total_budget1 != "" && $demand -> total_budget1 != 0) {
			if ($demand -> type_rent == 1)
				$where['rent_total >='] = $demand -> total_budget1;
			else
				$where['sale_total >='] = $demand -> total_budget1;
		}
		if ($demand -> total_budget2 != "" && $demand -> total_budget2 != 0) {
			if ($demand -> type_rent == 1)
				$where['rent_total <='] = $demand -> total_budget2;
			else
				$where['sale_total <='] = $demand -> total_budget2;
		}

		if ($demand -> budget_per_ping1 != "" && $demand -> budget_per_ping1 != 0) {
			if ($demand -> type_rent == 1)
				$where['rent_housing_rent >='] = $demand -> budget_per_ping1;
			else
				$where['sale_housing_prices >='] = $demand -> budget_per_ping1;
		}
		if ($demand -> budget_per_ping2 != "" && $demand -> budget_per_ping2 != 0) {
			if ($demand -> type_rent == 1)
				$where['rent_housing_rent <='] = $demand -> budget_per_ping2;
			else
				$where['sale_housing_prices <='] = $demand -> budget_per_ping2;
		}
		$this -> load -> model('object_model');
		$condition = "";
		foreach ($where as $key => $val) {
			if ($condition == "")
				$condition .= $key . "|" . $val;
			else
				$condition .= "---" . $key . "|" . $val;
		}
		$html["query_condition"] = $condition;
		$config['per_page'] = 15;
		$config['total_rows'] = $this -> object_model -> object_pair_count($where, $w);
		$config['url'] = '/pair/demand_ajax';
		$config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
		$limit = $config['per_page'] . ",0";
		$html['page'] = $config;
		$html['sequence'] = "";
		$html["result"] = $this -> object_model -> object_pair($where, $w, $limit);
		$this -> load -> model('object_state_model');
		$html["object_state"] = $this -> object_state_model -> get_object_state();
		$this -> load -> model('market_state_model');
		$html["market_state"] = $this -> market_state_model -> get_market_state();
		$this -> load -> model('building_name_model');
		$html["building_name"] = $this -> building_name_model -> get_building_name();
		$this -> load -> view('demand_analog_pairing', $html);
	}

	public function demand_analog_pairing_ajax($id = "") {
		$config['per_page'] = 15;
		$query_condition = $this -> input -> post('query_condition');
		$page = $this -> input -> post('page');
		$sequence = $this -> input -> post('sequence');
		$where = $this -> resolve($query_condition, "where");
		$p = $config['per_page'] * ($page - 1);
		$limit = $config['per_page'] . "," . $p;
		$html["result"] = $this -> object_model -> object_pair($where, $limit);
		$this -> load -> model('object_state_model');
		$html["object_state"] = $this -> object_state_model -> get_object_state();
		$this -> load -> model('market_state_model');
		$html["market_state"] = $this -> market_state_model -> get_market_state();
		$this -> load -> model('building_name_model');
		$html["building_name"] = $this -> building_name_model -> get_building_name();
		$this -> load -> view('demand_analog_pairing_ajax', $html);
	}

	public function demand_formal_pairing() {
		$demand_temporary_id = $this -> input -> post('demand_temporary_id');
		$this -> load -> model('demand_temporary_model');
		$demand_temporary = $this -> demand_temporary_model -> get_demand_info($demand_temporary_id);
		$data['type_rent'] = $demand_temporary -> type_rent;
		$data['type_sell'] = $demand_temporary -> type_sell;
		$data['object_usage'] = $demand_temporary -> object_usage;
		$data['name'] = $demand_temporary -> name;
		$data['mobile'] = $demand_temporary -> mobile;
		$data['home_phone'] = $demand_temporary -> home_phone;
		$data['company_phone'] = $demand_temporary -> company_phone;
		$data['home_address'] = $demand_temporary -> home_address;
		$data['building_name'] = $demand_temporary -> building_name;
		$data['company_address'] = $demand_temporary -> company_address;
		$data['company_name'] = $demand_temporary -> company_name;
		$data['email'] = $demand_temporary -> email;
		$data['contact'] = $demand_temporary -> contact;
		$data['contact_phone'] = $demand_temporary -> contact_phone;
		$data['building_area1'] = $demand_temporary -> building_area1;
		$data['building_area2'] = $demand_temporary -> building_area2;
		$data['parking_number'] = $demand_temporary -> parking_number;
		$data['acceptable_mechanical'] = $demand_temporary -> acceptable_mechanical;
		$data['indoor_area1'] = $demand_temporary -> indoor_area1;
		$data['indoor_area2'] = $demand_temporary -> indoor_area2;
		$data['land_area1'] = $demand_temporary -> land_area1;
		$data['land_area2'] = $demand_temporary -> land_area2;
		$data['key_areas1'] = $demand_temporary -> key_areas1;
		$data['key_areas2'] = $demand_temporary -> key_areas2;
		$data['specify_building1'] = $demand_temporary -> specify_building1;
		$data['specify_building2'] = $demand_temporary -> specify_building2;
		$data['sections1'] = $demand_temporary -> sections1;
		$data['sections2'] = $demand_temporary -> sections2;
		$data['object_name1'] = $demand_temporary -> object_name1;
		$data['object_name2'] = $demand_temporary -> object_name2;
		$data['mrt_line1'] = $demand_temporary -> mrt_line1;
		$data['mrt_start1'] = $demand_temporary -> mrt_start1;
		$data['mrt_until1'] = $demand_temporary -> mrt_until1;
		$data['mrt_line2'] = $demand_temporary -> mrt_line2;
		$data['mrt_start2'] = $demand_temporary -> mrt_start2;
		$data['mrt_until2'] = $demand_temporary -> mrt_until2;
		$data['range_east'] = $demand_temporary -> range_east;
		$data['range_north'] = $demand_temporary -> range_north;
		$data['range_west'] = $demand_temporary -> range_west;
		$data['range_south'] = $demand_temporary -> range_south;
		$data['park1'] = $demand_temporary -> park1;
		$data['park2'] = $demand_temporary -> park2;
		$data['school1'] = $demand_temporary -> school1;
		$data['school2'] = $demand_temporary -> school2;
		$data['circle1'] = $demand_temporary -> circle1;
		$data['circle2'] = $demand_temporary -> circle2;
		$data['administrative_area1'] = $demand_temporary -> administrative_area1;
		$data['administrative_area2'] = $demand_temporary -> administrative_area2;
		$data['total_budget1'] = $demand_temporary -> total_budget1;
		$data['total_budget2'] = $demand_temporary -> total_budget2;
		$data['budget_per_ping1'] = $demand_temporary -> budget_per_ping1;
		$data['budget_per_ping2'] = $demand_temporary -> budget_per_ping2;
		$data['year_of_construction'] = $demand_temporary -> year_of_construction;
		$data['object_case_type'] = $demand_temporary -> object_case_type;
		$data['windows'] = $demand_temporary -> windows;
		$data['facing_the_household'] = $demand_temporary -> facing_the_household;
		$data['building_facing'] = $demand_temporary -> building_facing;
		$data['designated_floors'] = $demand_temporary -> designated_floors;
		$data['object_styles'] = $demand_temporary -> object_styles;
		$data['public_facilities'] = $demand_temporary -> public_facilities;
		$data['accessories_with'] = $demand_temporary -> accessories_with;
		$data['management'] = $demand_temporary -> management;
		$data['number_of_rooms1'] = $demand_temporary -> number_of_rooms1;
		$data['number_of_rooms2'] = $demand_temporary -> number_of_rooms2;
		$data['number_of_hall1'] = $demand_temporary -> number_of_hall1;
		$data['number_of_hall2'] = $demand_temporary -> number_of_hall2;
		$data['number_of_toilet1'] = $demand_temporary -> number_of_toilet1;
		$data['number_of_toilet2'] = $demand_temporary -> number_of_toilet2;
		$data['number_of_suite1'] = $demand_temporary -> number_of_suite1;
		$data['number_of_suite2'] = $demand_temporary -> number_of_suite2;
		$data['partition_the'] = $demand_temporary -> partition_the;
		$data['floor_height'] = $demand_temporary -> floor_height;
		$data['customer_source'] = $demand_temporary -> customer_source;
		$data['required_level'] = $demand_temporary -> required_level;
		$data['remark'] = $demand_temporary -> remark;
		$data['currency'] = $demand_temporary -> currency;
		$data['area_unit'] = $demand_temporary -> area_unit;
		$data['remark'] = $demand_temporary -> remark;
		$data['status'] = $demand_temporary -> status;
		$data['invalid_description'] = $demand_temporary -> invalid_description;
		$data['front_man'] = $demand_temporary -> front_man;
		$data['head'] = $demand_temporary -> head;
		$data['original_broker'] = $demand_temporary -> original_broker;
		$data['broker'] = $demand_temporary -> broker;
		$this -> load -> model('demand_model');
		$return_result = $this -> demand_model -> add_demand($data);
		//echo $this->db->last_query();die();
		if ($return_result > 0) {
			$this -> load -> model('pair_model');
			$pair_data['demand_id'] = $return_result;
			$object_id = $this -> input -> post('object_id');
			foreach ($object_id as $key => $val) {
				$pair_data['object_id'] = $val;
				$this -> pair_model -> add($pair_data);
			}
			$this -> demand_temporary_model -> delete_demand($demand_temporary_id);
			redirect("/demand/data_query_results");
		} else {
			echo "插入失敗";
		}
	}

	//no use
	function object_match($id = "") {

		//$html=$this->init("單筆物件配對","jquery","table");
		$this -> load -> model('object_model');

		$object_info = $this -> object_model -> get_object_id($id);

		//print_r($object);
		//$w=array();

		$where = array();

		foreach ($object_info->result_array() as $row) {
			foreach ($row as $name => $value) {

				//echo $name. "=>".$value.'<BR>'.'<BR>';

				if ($name != 'object_id' AND $name != 'ctime')
					if (!empty($value) AND count($value) >= 1) {

						if ($name == 'type_sale') {
							$where["type_sell"] = $value;
						} else {

							$where[$name] = $value;
						}

						//echo $name .'=>'.$value;
						//echo "<br>";
					}
			}
		}

		//print_r($where);

		$math_res = $this -> object_model -> object_match_demand($where);
		//print_r($math_res);
		foreach ($math_res->result() as $row) {
			//print_r($row);
			//echo $row->front_man;
		}
	}

	public function shop_list777($set = 0, $set2 = 0) {
		//$set = 0, $set2 = 0) {
		/*
		 * set 是頁碼
		 * set2 是類別數
		 */
		//echo "set  = ".$set."<br>";
		//echo "set2 = ".$set2."<br>";
		/*
		 if (!empty($_GET["id"])) {
		 $newdata = array("page_se" => $_GET["id"]);
		 $this->session->set_userdata($newdata);
		 }

		 if ($set >= 1) {
		 $_GET["id"] = $set2;
		 }

		 $sse = $this->session->all_userdata();

		 //echo $sse["page_se"];
		 //echo " , ".$set2." , ".$set;
		 /* 商圈 影片~
		 $data['film'] = $this->about_model->about_film();
		 $cate_id = 0;
		 $all_shop_num = 0;
		 if (!empty($sse["page_se"])) {

		 $cate_id = $sse["page_se"];
		 $_SESSION['scate_id'] = $sse["page_se"];
		 $this->session->set_userdata('scate_id', $sse["page_se"]);
		 $all_shop_num = $this->bright_model->query_all_shop_cate_num($cate_id);
		 } else {

		 $all_shop_num = $this->bright_model->query_all_shop_cate_num2();
		 $cate_id = $this->session->userdata('scate_id');
		 }

		 $shop_num = $all_shop_num->row()->count;
		 $config['enable_query_strings'] = TRUE;
		 $config['total_rows'] = $shop_num;
		 //設置本頁路徑
		 $config['base_url'] = base_url() . 'index.php/bright/shop_list777/' . $set;
		 //設置每頁顯示記錄數
		 $config['per_page'] = '1';
		 //設置分頁導航條樣式
		 $config['full_tag_open'] = '<div id = "page_nav">';
		 $config['full_tag_close'] = '</div>';
		 $config['first_link'] = '首頁';
		 $config['last_link'] = '末頁';
		 $config['next_link'] = '下一頁>';
		 $config['prev_link'] = '<上一頁';
		 //應用設置
		 $this->pagination->initialize($config);
		 //設置查詢條件
		 $data['all_shop'] = $this->bright_model->query_all_shop_cate33($config['per_page'], $set, $sse["page_se"]);
		 $data['all_shop_img'] = $this->bright_model->query_all_shop_img();
		 //添加分頁導航條
		 $data["link"] = $this->pagination->create_links();
		 $data['shop_news'] = $this->bright_model->shop_news();
		 $data['hot_news'] = $this->bright_model->shop_hot_news();

		 $data['user_data'] = $this->session->all_userdata();

		 $this->load->view('bright/shop_list', $data);
		 */
	}

	function object() {


		$id = $this -> input -> get('id');
		if (empty($id))
			$id = 0;

		$page = $this -> input -> get('page');
		if (empty($page))
			$page = 0;

		$per_page = $this -> input -> get('per_page');
		if (empty($per_page))
			$per_page = 0;

		$html = $this -> init("單筆物件配對", "jquery", "table");

		$object_r  = $this -> object_model -> get_one_object_info($id);

		//$client_id = $object_r->row()->client_id;


		$html['object_info'] = $object_r;
		$test_flag = true;
		$object = $object_r -> row();
		$w = array();
		$where = array();
		$where_kaki = array();
		foreach ($object as $row => $_val) {

			if (!empty($_val)) {
				if (is_numeric($_val)) {
					if ((int)$_val != 0) {

						$where_kaki[$row] = $_val;
					}
				} else {
					$where_kaki[$row] = $_val;
				}
			}
		}

		$where_true = array();
		foreach ($where as $key => $val) {

			if (is_numeric($val)) {
				$new_val = (int)$val;
				if ($new_val >= 1) {
					$where_true[$key] = $val;
				}
			} else {

				$where_true[$key] = $val;
			}
		}

		$condition = "";
		foreach ($where as $key => $val) {
			if ($condition == "")
				$condition .= $key . "|" . $val;
			else
				$condition .= "---" . $key . "|" . $val;
		}

		$html["query_condition"] = $condition;

		$demand_data = $this -> demand_m -> object_match_demand($where_kaki);

		$config['base_url'] = base_url() . "pair/object?id=" . $id . "&page=" . $page;
		$config['first_link'] = "首頁";
		$config['last_link'] = "尾頁";
		$config['total_rows'] = $demand_data -> num_rows();
		//$data['dataLenght'];
		$config['per_page'] = 15;
		$config['page_query_string'] = TRUE;
		$this -> pagination -> initialize($config);
		$limit = $per_page . ' , ' . $config['per_page'];
		$html["victor_page"] = $this -> pagination -> create_links();

		/*         * *******對的*********** */

		$send_html_header = '<table border=2 align=center width=70% cellspacing=0 cellpadding=0>';
		$object_html_content = $this -> send_html -> ob_html($object);
		$send_html_footer = '</table>';

		$send_html;
		$send_html = $send_html_header . $object_html_content . $send_html_footer;

		$this -> object_model -> update_object_point_count($id);

		if ($demand_data -> num_rows > 0) {

			foreach ($demand_data->result() as $row) {

				$client_query = '';
				if (!empty($row -> broker)) {

					$client_query = $this -> users_model -> get_user_by_broker($row -> broker);
					if ($client_query -> num_rows >= 1) {

						$url = "?object_id=" . $id . "&demand_id=" . $row -> demand_id;
						$this -> pair_model -> insert_verify($url);

						$client_if = $client_query -> row();
						$demand_html_content = $this -> send_html -> de_html($row);

						$this -> demand_model -> update_demand_point_count($row -> demand_id);

						//$ob_sms = $this->send_html->ob_sms($object);
						//$de_sms = $this->send_html->de_sms($row);
						$verify_code = $this -> verfiy_show();
						$sms_content = $this -> send_html -> obj_dem_sms_body($object, $row, $verify_code);

						$this -> send_sms($client_if -> phone, $client_if -> name, $sms_content);
						$this -> send_mails($client_if -> email, '配對成功', $client_if -> name, 'title', $sms_content);
					}
				}
			}

		} else {

			$send_html .= '沒有配對到';
			$sms_content = $this -> send_html -> object_sms_no_pair2($object);
			$all_users = $this -> get_all_users();

			foreach ($all_users as $user) {

				$this -> send_mails($user -> email, '沒有配對到！！', $user -> name, '物件配需求', $sms_content);

				$this -> send_sms($user -> phone, $user -> name, $sms_content);

			}

		}

		$html["where_sql"] = $where_kaki;
		////////////////////////////////////////////////////////////////////////////
		$html["building_name"] = $this -> building_name_model -> get_all_new();		
		$html["emphasis_area"] = $this -> emphasis_area_model -> get_all_new();
		$html["address_city"] = $this -> address_city_model -> get_all_new();
		$html["address_county"] = $this -> address_county_model -> get_by_id('1');
		
		
		$html["users"] = $this -> base_data_lib -> get_all_users_array();
		$html["object_usages"] = $this -> base_data_lib -> get_object_usage_array();
		////////////////////////////////////////////////////////////////////////////




		$load_count=10;

		$page;
		$_page;
		if(empty($_GET["page"]) ){
			$page=0;
			$_page=0;
		}else{
			$_page= $_GET["page"];
			$page= ($_GET["page"]*$load_count)+1; 
		}

		$_start_num=$page;
		//object
		//echo $_start_num .'/,/'. $load_count;
		//die();
		$sql_schema = $this -> first_lib -> object_pair_demand_sql($where_kaki , $_start_num , $load_count);
		
		//echo $sql_schema;
		//die();
		$_result = $this -> pattern_model -> just_do_query($sql_schema);
		//print_r($_result);
		//die();
		$html["result"] = $_result;
		//跳窗	
		$demand_modal=array();
		$html["currency"] 	= $this ->base_data_lib ->get_currency_array();
		$html["area_unit"] 	= $this ->base_data_lib ->get_area_unit_array();

		$_data=$html;

		$this->load->model('client/contact','contact');

		foreach($html['result']->result() as $row){					
			$_data["demand"]=$row;
			$_data["page"] = $_page;
			$_data["client"]=$this->client_model->get_client_info($row->client_id);
			$_data["contact"]=$this->contact->get_contact_by_id($row->client_contact_id);
			$demand_modal[]=$this -> load -> view('object/demand_info' , $_data  , true);
		}		
		$html["show_box_infos"] = $demand_modal;
		//echo "($id , $page)";
		$html["_id"] = $id;	

		$html["page"] = $_page;
		//結果頁
		$html["result_page"] = $this->load->view('object/object_pair_ajax',$html,true);

		if (empty($object_r)) {
			$html["resu"] = '';
		}


		$this -> load -> view('object/object_pair', $html);

	}

	public function pf(){
		phpinfo();
	} 

	public function send_sms($phone, $name, $content) {

		$flag_ = true;

		if ($flag_) {

			$phone_number = '';
			$phone_codes = explode("-", $phone);

			foreach ($phone_codes as $phone => $codes) {

				$phone_number .= $codes;
			}

			$this -> load -> library('sms_lib');
			$SendGet = new Sms_lib();
			$this -> load -> model('sms_model');
			$data['to'] = $phone;
			$data['name'] = $name;
			$data['content'] = $content;
			$data['status'] = 'N';
			$return_result = $this -> sms_model -> add($data);
			//0988326003 ken
			//0980292123 a one
			$iconv_content1 = mb_convert_encoding($content, "Big5", "UTF8");
			//$to=explode(",",$data['to']);
			$i = 0;
			$SendGet -> HttpRequest('http://smexpress.mitake.com.tw:' .
			//'9600/'.
			//'SmSendGet.asp?'.
			'7002/' . 'SpLmGet?' . 'username=12956227&' . 'password=michael0935&' . 'dstaddr=' . $phone_number . '&' . 'DestName=' . $name . '&' . 'smbody=' . $iconv_content1);
			// 呼叫成員方法
			$sms_rs = 0;
			if ($this -> send_sms) {

				$SendGet -> sendRequest();
				$sms_result = $SendGet -> getResponseBody();
				//取回傳值
				$sms_rs = strpos($sms_result, 'statuscode=1');
			}

			if ($sms_rs > 1) {
				$data['id'] = $return_result;
				$data['status'] = "Y";
				$this -> sms_model -> update_status($data);
			}

		}

	}

	function send_mail($to, $sender, $name, $subject, $content) {

		$this -> load -> library('email');
		$config['mailtype'] = 'html';
		$this -> email -> initialize($config);

		$this -> load -> model('edm_model');
		$data['to'] = $to;
		$data['sender'] = $sender;
		$data['name'] = $name;
		$data['subject'] = $subject;
		$data['content'] = $content;
		$data['status'] = 'N';
		$return_result = $this -> edm_model -> add($data);

		if ($return_result > 0) {

			$this -> load -> library('email');
			$this -> email -> clear();
			$this -> email -> from($data['sender'], $data['name']);
			$this -> email -> from($data['sender'], $data['name']);
			$this -> email -> to($data['sender']);

			$this -> email -> bcc($data['to']);
			$this -> email -> subject($data['subject']);
			$this -> email -> message($data['content']);

			if ($this -> email -> send()) {
				$data['id'] = $return_result;
				$data['status'] = 'Y';
				$this -> edm_model -> update_status($data);

				//redirect("/send_edm/email");
			}
		} else {
			//echo "插入失敗";
		}
	}

	function object_jason() {

		$id = $this -> input -> get('id');
		if (empty($id))
			$id = 0;

		$page = $this -> input -> get('page');
		if (empty($page))
			$page = 0;

		$per_page = $this -> input -> get('per_page');
		if (empty($per_page))
			$per_page = 0;

		$html = $this -> init("單筆物件配對", "jquery", "table");

		$this -> load -> library('show_test');
		$this -> load -> model('object_model');
		$this -> load -> model('demand_model');

		$object_r = $this -> object_model -> get_one_object_info($id);

		$test_flag = true;
		$object = $object_r -> row();
		$w = array();
		$where = array();

		$where_kaki = array();

		foreach ($object as $row => $_val) {

			if (!empty($_val)) {

				if (is_numeric($_val)) {
					if ((int)$_val != 0) {

						$where_kaki[$row] = $_val;
					}
				} else {

					$where_kaki[$row] = $_val;
				}
			}
		}

		$where_true = array();
		foreach ($where as $key => $val) {

			if (is_numeric($val)) {
				$new_val = (int)$val;
				if ($new_val >= 1) {
					$where_true[$key] = $val;
				}
			} else {

				$where_true[$key] = $val;
			}
		}

		$condition = "";
		foreach ($where as $key => $val) {
			if ($condition == "")
				$condition .= $key . "|" . $val;
			else
				$condition .= "---" . $key . "|" . $val;
		}

		$html["query_condition"] = $condition;
		$this -> load -> model("demand_m");

		/*         * *******對的*********** */

		$this -> load -> library('pagination');
		$_row_num = $this -> demand_m -> object_match_demand($where_kaki);
		$config['base_url'] = base_url() . "pair/object?id=" . $id . "&page=" . $page;
		$config['first_link'] = "首頁";
		$config['last_link'] = "尾頁";
		$config['total_rows'] = $_row_num -> num_rows();
		//$data['dataLenght'];
		$config['per_page'] = 15;
		$config['page_query_string'] = TRUE;
		$this -> pagination -> initialize($config);
		$limit = $config['per_page'] . " , " . $per_page;
		$html["victor_page"] = $this -> pagination -> create_links();

		/*         * *******對的*********** */

		$html["where_sql"] = $where_kaki;
		$html["resu"] = $this -> demand_m -> object_match_demand($where_kaki, $limit);
	}

	function object_pp() {

		$id = $this -> input -> get('id');
		if (empty($id))
			$id = 0;

		$page = $this -> input -> get('page');
		if (empty($page))
			$page = 0;

		$per_page = $this -> input -> get('per_page');
		if (empty($per_page))
			$per_page = 0;

		$html = $this -> init("單筆物件配對", "jquery", "table");

		$this -> load -> library('show_test');
		$this -> load -> model('object_model');
		$this -> load -> model('demand_model');

		$object_r = $this -> object_model -> get_one_object_info($id);

		$test_flag = true;
		$object = $object_r -> row();
		$w = array();
		$where = array();

		$where_kaki = array();

		foreach ($object as $row => $_val) {

			if (!empty($_val)) {

				if (is_numeric($_val)) {
					if ((int)$_val != 0) {

						$where_kaki[$row] = $_val;
					}
				} else {

					$where_kaki[$row] = $_val;
				}
			}
		}

		$where_true = array();
		foreach ($where as $key => $val) {

			if (is_numeric($val)) {
				$new_val = (int)$val;
				if ($new_val >= 1) {
					$where_true[$key] = $val;
				}
			} else {

				$where_true[$key] = $val;
			}
		}

		$condition = "";
		foreach ($where as $key => $val) {
			if ($condition == "")
				$condition .= $key . "|" . $val;
			else
				$condition .= "---" . $key . "|" . $val;
		}

		$html["query_condition"] = $condition;
		$this -> load -> model("demand_m");

		/*         * *******對的*********** */

		$this -> load -> library('pagination');
		$_row_num = $this -> demand_m -> object_match_demand($where_kaki);
		$config['base_url'] = base_url() . "pair/object?id=" . $id . "&page=" . $page;
		$config['first_link'] = "首頁";
		$config['last_link'] = "尾頁";
		$config['total_rows'] = $_row_num -> num_rows();
		//$data['dataLenght'];
		$config['per_page'] = 15;
		$config['page_query_string'] = TRUE;
		$this -> pagination -> initialize($config);
		$limit = $config['per_page'] . " , " . $per_page;
		$html["victor_page"] = $this -> pagination -> create_links();

		/*         * *******對的*********** */

		$html["resu"] = $this -> demand_m -> object_match_demand($where_kaki, $limit);
		$this -> load -> view('object_pair', $html);
	}

	function object_pattern($sequence = null) {

		$html = $this -> init("單筆物件配對", "jquery", "table");

		$this -> load -> library('show_test');
		$this -> load -> model('object_model');
		$this -> load -> model('demand_model');

		//$object_r = $this->object_model->get_one_object_info($id);

		$test_flag = true;
		$object = $_POST;
		$w = array();
		$where = array();

		$where_kaki = array();

		foreach ($object as $row => $_val) {

			if ($row != "subsidiary" AND $row != "pubilc_set" AND $row != "main_build_area" AND $row != "inside_crid" AND $row != "bcc")
				if (!empty($_val)) {

					//echo $row .'=>'. $_val;
					//echo "<br>";
					//if(is_numeric($_val))
					if (is_numeric($_val)) {
						if ((int)$_val != 0) {

							$where_kaki[$row] = $_val;
						}
					} else {

						$where_kaki[$row] = $_val;
					}
				}
		}

		$this -> load -> model("demand_m");
		/*         * *******對的*********** */
		//$keyword='';
		$this -> load -> library('pagination');
		$config['base_url'] = '/pair/object_pattern';
		$q_res = $this -> demand_m -> object_match_demand($where_kaki);
		$config['total_rows'] = $q_res -> num_rows();
		$config['per_page'] = 15;
		$limit = $config['per_page'] . "," . $sequence;
		$this -> pagination -> initialize($config);
		/*         * *******對的*********** */

		$html["victor_page"] = $this -> pagination -> create_links();

		/*
		 $html["query_condition"] = '';//$condition;
		 $config['per_page'] = 15;
		 //$config['total_rows'] = $this->demand_model->demand_pair_count($where,$w);
		 //$config['url'] = '/pair/object_ajax';
		 //$config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
		 $limit = $config['per_page'] . ",0";
		 $html['page'] = $config;
		 $html['sequence'] = "";
		 */

		//echo $this->db->last_query();
		$html["resu"] = $this -> demand_m -> object_match_demand($where_kaki, $limit);
		//$html["result"]=$this->demand_model->demand_pair($where,$w,$limit);
		//$this->load->model('pair_model');
		//$this->pair_model->update_object($id);
		//echo $this->db->last_query();
		//$data['object_id']=$id;

		/*
		 foreach($html["result"] as $key=>$val){
		 $data['demand_id']=$val->demand_id;
		 $c=$this->pair_model->check_pair($data['demand_id'],$data['object_id']);
		 if($c==0)
		 $this->pair_model->add($data);
		 }
		 */
		//print_r($where_true);
		$this -> load -> view('object_pair', $html);
	}

	public function object_ajax($id = "") {
		
		$this -> load -> model('demand_model');
		$config['per_page'] = 15;
		$query_condition = $this -> input -> post('query_condition');
		$page = $this -> input -> post('page');
		$sequence = $this -> input -> post('sequence');
		$where = $this -> resolve($query_condition, "where");
		$p = $config['per_page'] * ($page - 1);
		$limit = $config['per_page'] . "," . $p;
		$html["result"] = $this -> demand_model -> demand_pair($where, $limit);
		$this -> load -> view('object_pair_ajax', $html);
		
	}

	public function object_analog_pairing($id = "") {
		
		$html = $this -> init("模擬物件配對", "jquery", "table");
		$html['object_temporary_id'] = $id;
		$this -> load -> model('object_temporary_model');
		$object = $this -> object_temporary_model -> get_object_info($id);
		$where = array();
		$w = array();
		if ($object -> type_sale != "") {
			if ($object -> type_sale == 1)
				$where['type_sell'] = 1;
			if ($object -> type_rent == 1)
				$where['type_rent'] = 1;
		}
		if ($object -> object_usage != "") {
			$where['object_usage'] = $object -> object_usage;
		}

		if ($object -> building_area != "") {
			$where['building_area1 <='] = $object -> building_area;
			$where['building_area2 >='] = $object -> building_area;
		}
		if ($object -> sale_parking != "" && $object -> sale_parking != 0) {
			$where['parking_number'] = $object -> sale_parking;
		}
		if ($object -> rent_parking != "" && $object -> sale_parking != 0) {
			$where['parking_number'] = $object -> rent_parking;
		}

		if ($object -> main_building_area != "") {
			$where['indoor_area1 <='] = $object -> main_building_area;
			$where['indoor_area2 >='] = $object -> main_building_area;
		}

		if ($object -> stakeholders_land_area != "" && $object -> stakeholders_land_area != "0.00") {
			$where['land_area1 <='] = $object -> stakeholders_land_area;
			$where['land_area2 >='] = $object -> stakeholders_land_area;
		}

		if ($object -> main_building_area) {
			$where['specify_building1 <='] = $object -> main_building_area;
			$where['specify_building2 >='] = $object -> main_building_area;
		}
		if ($object -> area != "") {
			$w['area'] = $object -> area;
		}
		/* if($object->sections!=""){
		 $w['sections'] = $object->sections;
		 }
		 if($object->object_name!=""){
		 $w['object_name'] = $object->object_name;
		 } */
		if ($object -> mrt_line1 != "") {
			$where['mrt_line1'] = $object -> mrt_line1;
		}

		if ($object -> mrt_start1 != "" && $object -> mrt_start1 != 0) {
			//$where['mrt1'] = $demand->mrt_start1;
		}

		if ($object -> mrt_until1 != "" && $object -> mrt_until1 != 0) {
			//$where['mrt1'] = $demand->mrt_until1;
		}

		if ($object -> mrt_line2 != "" && $object -> mrt_line2 != 0) {
			$where['mrt_line2'] = $object -> mrt_line2;
		}

		if ($object -> mrt_start2 != "" && $object -> mrt_start2 != 0) {
			//$where['mrt2'] = $demand->mrt_start2;
		}

		if ($object -> mrt_until2 != "" && $object -> mrt_until2 != 0) {
			//$where['mrt2'] = $demand->mrt_until2;
		}

		if ($object -> range_east != "" && $object -> range_east != 0) {
			//$where['location_map1'] = $object->range_east;
		}
		if ($object -> range_north != "" && $object -> range_north != 0) {
			//$where['location_map1'] = $object->range_north;
		}
		if ($object -> range_west != "" && $object -> range_west != 0) {
			//$where['location_map2'] = $object->range_west;
		}
		if ($object -> range_south != "" && $object -> range_south != 0) {
			//$where['location_map2'] = $object->range_south;
		}
		if ($object -> park1 != "" && $object -> park1 != 0) {
			$where['park1'] = $object -> park1;
		}
		if ($object -> park2 != "" && $object -> park2 != 0) {
			$where['park2'] = $object -> park2;
		}
		if ($object -> school_district_primary) {
			$where['school1'] = $object -> school_district_primary;
		}
		if ($object -> school_district_high) {
			$where['school2'] = $object -> school_district_high;
		}
		/*
		 if($object->administrative_area){
		 $w['administrative_area'] = $object->administrative_area;
		 } */
		if ($object -> reported_rate != "" && $object -> reported_rate != "0.00") {
			$where['reported_rate1 <='] = $object -> reported_rate;
			$where['reported_rate2 >='] = $object -> reported_rate;
		}
		if ($object -> year_of_construction != "" && $object -> year_of_construction != 0) {
			$where['year_of_construction'] = $object -> year_of_construction;
		}
		if ($object -> object_case_type != "" && $object -> object_case_type != 0) {
			//$where['object_case_type'] = $object->object_case_type;
		}
		if ($object -> year_of_construction != "" && $object -> year_of_construction != 0) {
			$where['year_of_construction'] = $object -> year_of_construction;
		}
		if ($object -> windows != "" && $object -> windows != 0) {
			$where['windows'] = $object -> windows;
		}
		/* if($object->facing_the_household!="" && $object->facing_the_household!=0){
		 $where['household_door'] = $object->facing_the_household;
		 }
		 if($object->building_facing!="" && $object->building_facing!=0){
		 $where['building_entrance'] = $object->building_facing;
		 } */
		if ($object -> designated_floors1 != "" && $object -> designated_floors1 != 0) {
			//$w['floor'] = $object->designated_floors1;
		}

		if ($object -> object_styles != "" && $object -> object_styles != 0) {
			//where['windows'] = $object->object_styles;
		}
		if ($object -> public_facilities != "" && $object -> public_facilities != 0) {
			$where['public_facilities'] = $object -> public_facilities;
		}

		/* if($object->accessories_with!="" && $object->accessories_with!=0){
		 $where['rent_ancillary'] = $object->accessories_with;
		 } */
		if ($object -> management != "" && $object -> management != 0) {
			$where['management'] = $object -> management;
		}

		if ($object -> indoor_pattern_house != "" && $object -> indoor_pattern_house != 0) {
			$where['number_of_rooms1 <='] = $object -> indoor_pattern_house;
			$where['number_of_rooms2 >='] = $object -> indoor_pattern_house;
		}
		if ($object -> number_of_rooms2 != "" && $object -> number_of_rooms2 != 0) {

		}

		if ($object -> indoor_pattern_hall != "" && $object -> indoor_pattern_hall != 0) {
			$where['number_of_hall1 <='] = $object -> indoor_pattern_hall;
			$where['number_of_hall2 >='] = $object -> indoor_pattern_hall;
		}

		if ($object -> indoor_pattern_guard != "" && $object -> indoor_pattern_guard != 0) {
			$where['number_of_toilet1 <='] = $object -> indoor_pattern_guard;
			$where['number_of_toilet2 >='] = $object -> indoor_pattern_guard;
		}

		if ($object -> indoor_pattern_suite != "" && $object -> indoor_pattern_suite != 0) {
			$where['number_of_suite1 <='] = $object -> indoor_pattern_suite;
			$where['number_of_suite2 >='] = $object -> indoor_pattern_suite;
		}
		if ($object -> land_use_zoning != "" && $object -> land_use_zoning != 0) {
			$where['partition_the'] = $object -> land_use_zoning;
		}
		if ($object -> floor_height != "" && $object -> floor_height != 0) {
			$where['floor_height'] = $object -> floor_height;
		}
		if ($object -> rent_total != "" && $object -> rent_total != 0) {
			$where['total_budget1 <='] = $object -> rent_total;
			$where['total_budget2 >='] = $object -> rent_total;
		}

		if ($object -> sale_total != "" && $object -> sale_total != 0) {
			$where['total_budget1 <='] = $object -> sale_total;
			$where['total_budget2 >='] = $object -> sale_total;
		}

		if ($object -> rent_housing_rent != "" && $object -> rent_housing_rent != 0) {
			$where['budget_per_ping1 <='] = $object -> rent_housing_rent;
			$where['budget_per_ping2 >='] = $object -> rent_housing_rent;
		}

		if ($object -> sale_housing_prices != "" && $object -> sale_housing_prices != 0) {
			$where['budget_per_ping1 <='] = $object -> sale_housing_prices;
			$where['budget_per_ping2 >='] = $object -> sale_housing_prices;
		}

		$condition = "";
		foreach ($where as $key => $val) {
			if ($condition == "")
				$condition .= $key . "|" . $val;
			else
				$condition .= "---" . $key . "|" . $val;
		}
		$this -> load -> model('demand_model');
		$html["query_condition"] = $condition;
		$config['per_page'] = 15;
		$config['total_rows'] = $this -> demand_model -> demand_pair_count($where, $w);
		$config['url'] = '/pair/object_ajax';
		$config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
		$limit = $config['per_page'] . ",0";
		$html['page'] = $config;
		$html['sequence'] = "";
		$html["result"] = $this -> demand_model -> demand_pair($where, $w, $limit);
		//echo $this->db->last_query();
		$this -> load -> view('object_analog_pairing', $html);
	}

	public function object_analog_pairing_ajax($id = "") {
		$this -> load -> model('demand_model');
		$config['per_page'] = 15;
		$query_condition = $this -> input -> post('query_condition');
		$page = $this -> input -> post('page');
		$sequence = $this -> input -> post('sequence');
		$where = $this -> resolve($query_condition, "where");
		$p = $config['per_page'] * ($page - 1);
		$limit = $config['per_page'] . "," . $p;
		$html["result"] = $this -> demand_model -> demand_pair($where, $limit);
		$this -> load -> view('object_analog_pairing_ajax', $html);
	}

	public function object_formal_pairing() {
		
		$object_temporary_id = $this -> input -> post('object_temporary_id');
		
		$object_temporary = $this -> object_temporary_model -> get_object_info($object_temporary_id);
		
		$data['market_survey_no'] = $object_temporary -> market_survey_no;
		$data['object_no'] = $object_temporary -> object_no;
		$data['object_state'] = $object_temporary -> object_state;
		$data['type_rent'] = $object_temporary -> type_rent;
		$data['type_sale'] = $object_temporary -> type_sale;
		$data['owner'] = $object_temporary -> owner;
		$data['owner_mobile'] = $object_temporary -> owner_mobile;
		$data['owner_phone'] = $object_temporary -> owner_phone;
		$data['market_research_status'] = $object_temporary -> market_research_status;
		$data['principal'] = $object_temporary -> principal;
		$data['principal_mobile'] = $object_temporary -> principal_mobile;
		$data['principal_phone'] = $object_temporary -> principal_phone;
		$data['case_name'] = $object_temporary -> case_name;
		$data['building_name'] = $object_temporary -> building_name;
		$data['owner'] = $object_temporary -> owner;
		$data['object_usage'] = $object_temporary -> object_usage;
		$data['object_styles'] = $object_temporary -> object_styles;
		$data['house_building'] = $object_temporary -> house_building;
		$data['area'] = $object_temporary -> area;
		$data['build_number'] = $object_temporary -> build_number;
		$data['sale_total'] = $object_temporary -> sale_total;
		$data['sale_parking'] = $object_temporary -> sale_parking;
		$data['sale_housing_prices'] = $object_temporary -> sale_housing_prices;
		$data['sale_housing_unit_price'] = $object_temporary -> sale_housing_unit_price;
		$data['sale_parking_price'] = $object_temporary -> sale_parking_price;
		$data['sale_parking_unit_price'] = $object_temporary -> sale_parking_unit_price;
		$data['sale_land_unit_price'] = $object_temporary -> sale_land_unit_price;
		$data['rent_total'] = $object_temporary -> rent_total;
		$data['rent_tax'] = $object_temporary -> rent_tax;
		$data['rent_parking'] = $object_temporary -> rent_parking;
		$data['rent_housing_rent'] = $object_temporary -> rent_housing_rent;
		$data['rent_parking_space_rentals'] = $object_temporary -> rent_parking_space_rentals;
		$data['rent_rent_per_ping'] = $object_temporary -> rent_rent_per_ping;
		$data['rent_monthly_rent_parking_spaces'] = $object_temporary -> rent_monthly_rent_parking_spaces;
		$data['rent_deposit_month'] = $object_temporary -> rent_deposit_month;
		$data['rent_deposit_money'] = $object_temporary -> rent_deposit_money;
		$data['rent_min'] = $object_temporary -> rent_min;
		$data['rent_max'] = $object_temporary -> rent_max;
		$data['rent_lease_hike'] = $object_temporary -> rent_lease_hike;
		$data['rent_rent_free_period_decoration'] = $object_temporary -> rent_rent_free_period_decoration;
		$data['rent_ancillary'] = $object_temporary -> rent_ancillary;
		$data['rent_notarization'] = $object_temporary -> rent_notarization;
		$data['building_area'] = $object_temporary -> building_area;
		$data['main_building_area'] = $object_temporary -> main_building_area;
		$data['subsidiary_building_area'] = $object_temporary -> subsidiary_building_area;
		$data['postulate'] = $object_temporary -> postulate;
		$data['total_weight_shaped_area'] = $object_temporary -> total_weight_shaped_area;
		$data['with_parking_number'] = $object_temporary -> with_parking_number;
		$data['parking_area'] = $object_temporary -> parking_area;
		$data['parking_area_number'] = $object_temporary -> parking_area_number;
		$data['use_area'] = $object_temporary -> use_area;
		$data['postulate_than'] = $object_temporary -> postulate_than;
		$data['base_is_located1'] = $object_temporary -> base_is_located1;
		$data['base_is_located2'] = $object_temporary -> base_is_located2;
		$data['to_no'] = $object_temporary -> to_no;
		$data['base_area'] = $object_temporary -> base_area;
		$data['scope_of_rights1'] = $object_temporary -> scope_of_rights1;
		$data['scope_of_rights2'] = $object_temporary -> scope_of_rights2;
		$data['stakeholders_land_area'] = $object_temporary -> stakeholders_land_area;
		$data['present_value'] = $object_temporary -> present_value;
		$data['total_present_value1'] = $object_temporary -> total_present_value1;
		$data['total_present_value2'] = $object_temporary -> total_present_value2;
		$data['land_use_zoning'] = $object_temporary -> land_use_zoning;
		$data['volume_rate'] = $object_temporary -> volume_rate;
		$data['building_coverage_ratio'] = $object_temporary -> building_coverage_ratio;
		$data['monthly_management_fee'] = $object_temporary -> monthly_management_fee;
		$data['management_fees_per_ping'] = $object_temporary -> management_fees_per_ping;
		$data['management'] = $object_temporary -> management;
		$data['indoor_pattern_house'] = $object_temporary -> indoor_pattern_house;
		$data['indoor_pattern_hall'] = $object_temporary -> indoor_pattern_hall;
		$data['indoor_pattern_guard'] = $object_temporary -> indoor_pattern_guard;
		$data['indoor_pattern_suite'] = $object_temporary -> indoor_pattern_suite;
		$data['windows'] = $object_temporary -> windows;
		$data['household_door'] = $object_temporary -> household_door;
		$data['building_entrance'] = $object_temporary -> building_entrance;
		$data['households'] = $object_temporary -> households;
		$data['building_households'] = $object_temporary -> building_households;
		$data['total_towers'] = $object_temporary -> total_towers;
		$data['total_households'] = $object_temporary -> total_households;
		$data['floor'] = $object_temporary -> floor;
		$data['floor_plan1'] = $object_temporary -> floor_plan1;
		$data['floor_plan2'] = $object_temporary -> floor_plan2;
		$data['number_of_lifts1'] = $object_temporary -> number_of_lifts1;
		$data['number_of_lifts2'] = $object_temporary -> number_of_lifts2;
		$data['facing_the_lane'] = $object_temporary -> facing_the_lane;
		$data['use_status'] = $object_temporary -> use_status;
		$data['lease_maturity'] = $object_temporary -> lease_maturity;
		$data['lessee_by_industry'] = $object_temporary -> lessee_by_industry;
		$data['floor_height'] = $object_temporary -> floor_height;
		$data['building_exterior'] = $object_temporary -> building_exterior;
		$data['main_building'] = $object_temporary -> main_building;
		$data['building_completion_date'] = $object_temporary -> building_completion_date;
		$data['year_of_construction'] = $object_temporary -> year_of_construction;
		$data['annual_rent'] = $object_temporary -> annual_rent;
		$data['reported_rate'] = $object_temporary -> reported_rate;
		$data['construction_company'] = $object_temporary -> construction_company;
		$data['construction_license_no'] = $object_temporary -> construction_license_no;
		$data['look_mode'] = $object_temporary -> look_mode;
		$data['key_management'] = $object_temporary -> key_management;
		$data['location_map1'] = $object_temporary -> location_map1;
		$data['location_map2'] = $object_temporary -> location_map2;
		$data['currency'] = $object_temporary -> currency;
		$data['area_unit'] = $object_temporary -> area_unit;
		$data['bank_set_total_amount'] = $object_temporary -> bank_set_total_amount;
		$data['loan_bank'] = $object_temporary -> loan_bank;
		$data['other_settings_amount'] = $object_temporary -> other_settings_amount;
		$data['bring_own_money'] = $object_temporary -> bring_own_money;
		$data['estimated_loan'] = $object_temporary -> estimated_loan;
		$data['environmental_features'] = $object_temporary -> environmental_features;
		$data['object_features'] = $object_temporary -> object_features;
		$data['public_facilities'] = $object_temporary -> public_facilities;
		$data['fire_and_security'] = $object_temporary -> fire_and_security;
		$data['circle1'] = $object_temporary -> circle1;
		$data['circle2'] = $object_temporary -> circle2;
		$data['park1'] = $object_temporary -> park1;
		$data['park2'] = $object_temporary -> park2;
		$data['mrt_line1'] = $object_temporary -> mrt_line1;
		$data['mrt1'] = $object_temporary -> mrt1;
		$data['mrt_line2'] = $object_temporary -> mrt_line2;
		$data['mrt2'] = $object_temporary -> mrt2;
		$data['school_district_primary'] = $object_temporary -> school_district_primary;
		$data['school_district_high'] = $object_temporary -> school_district_high;
		$data['school_district_senior'] = $object_temporary -> school_district_senior;
		$data['spaces_where_floor'] = $object_temporary -> spaces_where_floor;
		$data['parking_numbers'] = $object_temporary -> parking_numbers;
		$data['parking_entrance_height'] = $object_temporary -> parking_entrance_height;
		$data['public_parking_spaces1'] = $object_temporary -> public_parking_spaces1;
		$data['public_parking_spaces2'] = $object_temporary -> public_parking_spaces2;
		$data['separate_parking1'] = $object_temporary -> separate_parking1;
		$data['separate_parking2'] = $object_temporary -> separate_parking2;
		$data['car_park_management_fee'] = $object_temporary -> car_park_management_fee;
		$data['total_management_fee_parking'] = $object_temporary -> total_management_fee_parking;
		$data['parking_type1'] = $object_temporary -> parking_type1;
		$data['parking_number1'] = $object_temporary -> parking_number1;
		$data['parking_price1'] = $object_temporary -> parking_price1;
		$data['parking_type2'] = $object_temporary -> parking_type2;
		$data['parking_number2'] = $object_temporary -> parking_number2;
		$data['parking_price2'] = $object_temporary -> parking_price2;
		$data['use_parking_number'] = $object_temporary -> use_parking_number;
		$data['ballot'] = $object_temporary -> ballot;
		$data['internet'] = $object_temporary -> internet;
		$data['valuation_date'] = $object_temporary -> valuation_date;
		$data['valuation_date'] = $object_temporary -> valuation_date;
		$data['appraisal_agencies'] = $object_temporary -> appraisal_agencies;
		$data['transaction_prices_near'] = $object_temporary -> transaction_prices_near;
		$data['bcc'] = $object_temporary -> bcc;
		$data['status'] = $object_temporary -> status;
		$data['invalid_description'] = $object_temporary -> invalid_description;
		$data['remark'] = $object_temporary -> remark;
		$data['front_man'] = $object_temporary -> front_man;
		$data['head'] = $object_temporary -> head;
		$data['assigned_date'] = $object_temporary -> assigned_date;
		$data['broker'] = $object_temporary -> broker;
		
		if ($object_temporary -> object_case_type != "")
			$data['object_case_type'] = $object_temporary -> object_case_type;
		$return_result = $this -> object_model -> add_object($data);
		//echo $this->db->last_query();die();
		if ($return_result > 0) {
			$this -> load -> model('pair_model');
			$pair_data['object_id'] = $return_result;
			$demand_id = $this -> input -> post('demand_id');
			foreach ($demand_id as $key => $val) {
				$pair_data['demand_id'] = $val;
				$this -> pair_model -> add($pair_data);
			}
			$this -> object_temporary_model -> delete_object($object_temporary_id);
			redirect("/object/data_query_results");
		} else {
			echo "插入失敗";
		}
	}

	public function resolve($query_condition, $type = "") {
		$keyword = "";
		$where = array();
		$e = explode('%%', $query_condition);
		if (count($e) > 1) {
			$e0 = explode('|', $e[0]);
			$keyword = $e0[1];
			$e1 = explode('---', $e[1]);
			if (count($e1) > 1) {
				foreach ($e1 as $key => $val) {
					$e2 = explode('|', $val);
					$where[$e2[0]] = $e2[1];
				}
			} else {
				$e2 = explode('|', $val);
				$where[$e2[0]] = $e2[1];
			}
		} else {
			$e1 = explode('---', $e[0]);
			if (count($e1) > 1) {
				foreach ($e1 as $key => $val) {
					$e2 = explode('|', $val);
					$where[$e2[0]] = $e2[1];
				}
			} else {
				if ($e[0] != "") {
					$e2 = explode('|', $e[0]);
					$where[$e2[0]] = $e2[1];
				}
			}
		}
		if ($type == "where")
			return $where;
		else
			return $keyword;
	}

	public function ajax_pair_result() {

		$type = $_POST['type']?$_POST['type']:'';
		//print_r($_POST);
		//$type = '';//$_POST['type'];
		/*********************************************************/
		//面積
		$building_area1 = $this -> input -> post('building_area1');
		$building_area2 = $this -> input -> post('building_area2');

		$land_area1 = $this -> input -> post('land_area1');
		$land_area2 = $this -> input -> post('land_area2');

		//$indoor_area1 = $this -> input -> post('indoor_area1');
		//$indoor_area2 = $this -> input -> post('indoor_area2');
		/*********************************************************/

		/*********************************************************/
		//價格
		$total_budget1 = $this -> input -> post('total_budget1');
		$total_budget2 = $this -> input -> post('total_budget2');

		$budget_per_ping1 = $this -> input -> post('budget_per_ping1');
		$budget_per_ping2 = $this -> input -> post('budget_per_ping2');
		/*********************************************************/

		/*********************************************************/
		//區域－路段
		$key_areas1 = $this -> input -> post('key_areas1');
		$key_areas2 = $this -> input -> post('key_areas2');

		$specify_building1 = $this -> input -> post('specify_building1');
		$specify_building2 = $this -> input -> post('specify_building2');

		$sections1 = $this -> input -> post('sections1');
		$sections2 = $this -> input -> post('sections2');
		/*********************************************************/

		//-------------------------------------------------------------------

		/*********************************************************/
		//object pair demand
		//面積
		$building_area = $this -> input -> post('building_area');
		$stakeholders_land_area = $this -> input -> post('stakeholders_land_area');
		$main_building_area = $this -> input -> post('main_building_area');
		/*********************************************************/

		/*********************************************************/
		//價格
		$sale_total = $this -> input -> post('sale_total');
		$sale_housing_unit_price = $this -> input -> post('sale_housing_unit_price');
		/*********************************************************/

		/*********************************************************/
		//區域－路段
		$area = $this -> input -> post('area');
		$building_name = $this -> input -> post('building_name');
		$house_building = $this -> input -> post('house_building');
		/*********************************************************/

		
		//入
		$facing_array = $this->base_data_lib->get_windows_array();
		$object_usage_array = $this->base_data_lib->get_object_usage_array();

		
		//print_r($facing_array);

		$windows 				= $_POST["windows"]?$_POST["windows"]:'';
	    $facing_the_household 	= !empty($_POST["facing_the_household"])?$_POST["facing_the_household"]:'';
		$building_facing 		= !empty($_POST["building_facing"])?$_POST["building_facing"]:'';

		$windows_more=array();
		$windows_arr='';
		$facing_the_household_arr='';
		$building_facing_arr='';

		if(!empty($windows)){
			$windows_arr = $this->pair_lib->facing_pattern($windows ,$facing_array );								
		}

		if(!empty($facing_the_household)){
			$facing_the_household_arr = $this->pair_lib->facing_pattern($facing_the_household ,$facing_array );								
		}

		if(!empty($building_facing)){
			$building_facing_arr = $this->pair_lib->facing_pattern($building_facing ,$facing_array );								
		}


		//print_r($_POST);	
		//print_r($windows_arr);	
		//print_r($object_usage_array);	



		$object_usage 			= $_POST["object_usage"]?$_POST["object_usage"]:'';
		$object_usage_arr;
		if(!empty($object_usage)){
			//$object_usage_arr = $this->pair_lib->object_usage_pattern($object_usage ,$object_usage_array );								
		}
		

		$load_count=10;
		$page;		
		$_page;

		if(empty($_POST["page"]) ){
			$page=0;
			$_page=0;
		}else{
			$_page= $_POST["page"];
			$page= ($_POST["page"]*$load_count)+1; 
		}

		$_start_num=$page;
		
		//$sql_schema = $this -> first_lib -> object_pair_demand_sql($where_kaki , $_start_num , $load_count);
		//echo '['.$_page.']';
		//echo $type;
		if ('demand_pair_object' == $type) {
				


			
			$demand_pair_object_data = array();
			$demand_pair_object_data = array('building_area1' => ($_POST["building_area_flag"] == 'true') ? $building_area1 : '', 
											'building_area2' => ($_POST["building_area_flag"] == 'true') ? $building_area2 : '', 
											'land_area1' => ($_POST["land_area_flag"] == 'true') ? $land_area1 : '', 
											'land_area2' => ($_POST["land_area_flag"] == 'true') ? $land_area2 : '', 
											//'indoor_area1' => ($_POST["indoor_area_flag"] == 'true') ? $indoor_area1 : '', 
											//'indoor_area2' => ($_POST["indoor_area_flag"] == 'true') ? $indoor_area2 : '', 
											'total_budget1' => ($_POST["total_budget_flag"] == 'true') ? $total_budget1 : '', 
											'total_budget2' => ($_POST["total_budget_flag"] == 'true') ? $total_budget2 : '', 
											'budget_per_ping1' => ($_POST["budget_per_ping_flag"] == 'true') ? $budget_per_ping1 : '',
											'budget_per_ping2' => ($_POST["budget_per_ping_flag"] == 'true') ? $budget_per_ping2 : '',
											 
											'key_areas1' => ($_POST["key_areas_flag"] == 'true') ? $key_areas1 : '', 
											'key_areas2' => ($_POST["key_areas_flag"] == 'true') ? $key_areas2 : '',
											
											'specify_building1' => ($_POST["specify_building_flag"] == 'true') ? $specify_building1 : '', 
											'specify_building2' => ($_POST["specify_building_flag"] == 'true') ? $specify_building2 : '', 
											'sections1' => ($_POST["sections_flag"] == 'true') ? $sections1 : '', 
											'sections2' => ($_POST["sections_flag"] == 'true') ? $sections2 : '',
											
																						
											//其他
											//車位數
											'parking_number' => ($_POST["parking_number_flag"] == 'true') ? $_POST["parking_number"] : '',
											
											//房數
											'number_of_rooms1' => ($_POST["number_of_rooms_flag"] == 'true') ? $_POST["number_of_rooms1"] : '',
											'number_of_rooms2' => ($_POST["number_of_rooms_flag"] == 'true') ? $_POST["number_of_rooms2"] : '',
																						
											//廳
											'number_of_hall1' => ($_POST["number_of_hall_flag"] == 'true') ? $_POST["number_of_hall1"] : '',
											'number_of_hall2' => ($_POST["number_of_hall_flag"] == 'true') ? $_POST["number_of_hall2"] : '',
											
											//衛
											'number_of_toilet1' => ($_POST["number_of_toilet_flag"] == 'true') ? $_POST["number_of_toilet1"] : '',
											'number_of_toilet2' => ($_POST["number_of_toilet_flag"] == 'true') ? $_POST["number_of_toilet2"] : '',
											
											//套房
											'number_of_suite1' => ($_POST["number_of_suite_flag"] == 'true') ? $_POST["number_of_suite1"] : '',
											'number_of_suite2' => ($_POST["number_of_suite_flag"] == 'true') ? $_POST["number_of_suite2"] : '',
											
											//售
											'type_sell' => (!empty($_POST['_type']) AND $_POST['_type'] =="type_sell"  ) ? 1 : '',											
											//租												
											'type_rent' => (!empty($_POST['_type']) AND $_POST['_type'] == "type_rent" ) ? 1 : '',
											//物件用途												
											'object_usage' => ($_POST["object_usage_flag"] == 'true' ) ? $_POST["object_usage"] : '',
											
											//屋況
											//指定樓層
											'designated_floors1' => ($_POST["designated_floors_flag"] == 'true') ? $_POST["designated_floors1"] : '',
											'designated_floors2' => ($_POST["designated_floors_flag"] == 'true') ? $_POST["designated_floors2"] : '',											
											//屋齡
											'year_of_construction1' => ($_POST["year_of_construction_flag"] == 'true') ? $_POST["year_of_construction1"] : '',
											'year_of_construction2' => ($_POST["year_of_construction_flag"] == 'true') ? $_POST["year_of_construction2"] : '',
											//落地窗座向
											'windows' => ($_POST["windows_flag"] == 'true') ? $_POST["windows"]  : '',
											//該戶座向
											'facing_the_household' => ($_POST["facing_the_household_flag"] == 'true') ? $_POST["facing_the_household"] : '',
											//大樓座向
											'building_facing' => ($_POST["building_facing_flag"] == 'true') ? $_POST["building_facing"] : '',											
											);
											
																					//$facing_the_household_arr;
																					//$building_facing_arr;
			//print_r($_POST);
			foreach($demand_pair_object_data as $name=>$val){

				if(!empty($val) ){
					//測試顯示									
					//echo $name.'=>'.$val.'<br>';
				}
				
			}
			//print_r($demand_pair_object_data);
			$this -> demand_pair_object_result($demand_pair_object_data ,
												$_start_num , $load_count ,$_page);

		}

		
		
		//物件＝配－->需求
		if ('object_pair_demand' == $type) {
			
			$object_usage 	   =$this->input->post('object_usage');
			$windows 		   =$this->input->post('windows');
			$building_entrance =$this->input->post('building_entrance');
			$household_door	   =$this->input->post('household_door');
			
			$object_usage_arr='';
			if(!empty($object_usage)){						
				$object_usage_arr = $this->pair_lib->object_usage_pattern($object_usage ,$object_usage_array );					
			}
			
			
			
			
			//print_r($windows_arr2);
			//echo count($windows_arr2);
			
			//print_r($facing_the_household_arr);
			//echo count($facing_the_household_arr);

			$object_pair_demand_data = array();
			$object_pair_demand_data = array(
											'building_area' => ($_POST["building_area_flag"] == 'true') ? $_POST["building_area"] : '', 
											'stakeholders_land_area' => ($_POST["stakeholders_land_area_flag"] == 'true') ? $stakeholders_land_area : '', 
											//'main_building_area' => ($_POST["main_building_area_flag"] == 'true') ? $main_building_area : '', 
											'sale_total' => ($_POST["sale_total_flag"] == 'true') ? $sale_total : '', 
											'sale_housing_unit_price' => ($_POST["sale_housing_unit_price_flag"] == 'true') ? $sale_housing_unit_price : '', 
											'area' => ($_POST["area_flag"] == 'true') ? $area : '', 
											'building_name' => ($_POST["building_name_flag"] == 'true') ? $building_name : '', 
											'house_building' => ($_POST["house_building_flag"] == 'true') ? $house_building : '',
											//售
											'type_sale' => (!empty($_POST["_type"])  AND ($_POST["_type"])=='type_sale') ? 1 : '',
											//租
											'type_rent' => (!empty($_POST["_type"]) AND ($_POST["_type"])=='type_rent' ) ? 1 : '',
											//物件用途
											'object_usage' => ($_POST["object_usage_flag"] == 'true') ? $_POST["object_usage"] : '',
											//屋況樓層
											//屋齡 
											'year_of_construction' => ($_POST["year_of_construction_flag"] == 'true') ? $_POST["year_of_construction"] : '',
											// 指定樓層 
											'floor' => ($_POST["floor_flag"] == 'true') ? $_POST["floor"] : '',
											//落地窗座向 
											'windows' => ($_POST["windows_flag"] == 'true' ) ? $_POST["windows"] : '',
											// 該戶座向 
											'household_door' => ($_POST["household_door_flag"] == 'true' ) ? $_POST["household_door"]: '',
											//大樓座向 
											'building_entrance' => ($_POST["building_entrance_flag"] == 'true' ) ? $_POST["building_entrance"]: '',
											//其他條件
											//車位數 
											'with_parking_number' => ($_POST["parking_area_number_flag"] == 'true') ? $_POST["parking_area_number"] : '',
											// 房數 
											'indoor_pattern_house' => ($_POST["indoor_pattern_house_flag"] == 'true') ? $_POST["indoor_pattern_house"] : '',
											//廳數 
											'indoor_pattern_hall' => ($_POST["indoor_pattern_hall_flag"] == 'true') ? $_POST["indoor_pattern_hall"] : '',
											// 衛數 
											'indoor_pattern_guard' => ($_POST["indoor_pattern_guard_flag"] == 'true') ? $_POST["indoor_pattern_guard"] : '',
											// 套房數 
											'indoor_pattern_suite' => ($_POST["indoor_pattern_suite_flag"] == 'true') ? $_POST["indoor_pattern_suite"] : '',											 
											);
			//echo $_POST["_type"];
			//echo '<br>';
			//echo '<br>';
			//echo '<br>';
			// $_start_num , $load_count);
			$this -> object_pair_demand_result($object_pair_demand_data ,
												$_start_num , $load_count ,$_page);
		}

	}

	public function object_pair_demand_result($data , $_start_num , $load_count , $_page) {

		//產生物件配需求的－＞sql
		////$sql_schema = $this -> first_lib -> object_pair_demand_sql($where_kaki , $_start_num , $load_count);
		$html=$this->base_data_lib->collcet_object_market_ajax();
		$sql_schema = $this -> pattern_lib -> object_pair_demand_sql($data);
		//echo '<br>';		
		//echo 
		$sql_schema = $sql_schema . " limit $_start_num , $load_count";
		$_result = $this -> pattern_model -> just_do_query($sql_schema);
		$html['result'] = $_result;
		
		$_data=$html;
		//$_data["currency"]  	= $this -> base_data_lib	-> get_currency_array();

		$demand_modal=array();
		foreach($html['result']->result() as $row){	

			$_data["demand"]			=$row;
			$_data["client"]			=$this->client_model->get_client_info($row->client_id);
			$_data["required_level"] 	= $this ->base_data_lib ->get_required_level_model_array();
			$_data["contact"]			=$this->contact->get_contact_by_id($row->client_contact_id);

			$demand_modal[]=$this -> load -> view('object/demand_info' , $_data  , true);
		}		
		
		foreach($demand_modal as $kk=>$vv){
			echo $vv;
			
		}
		$html["page"]=$_page;

		$this -> load -> view('object/headpair/object_pair_ajax', $html);

	}

	public function demand_pair_object_result($data , $_start_num , $load_count , $_page) {
		
		$html = $this->base_data_lib->collcet_object_market_ajax();
		/*********************************************************************************/		
		$html["currency"]  = $this ->base_data_lib->get_currency_array();
		$html["object_state"]  = $this -> object_state_model   -> get_object_state();		
		$html["market_state"]  = $this -> market_state_model   -> get_market_state();		
		$html["building_name"] = $this -> building_name_model  -> get_building_name();		
		$html["parking_type"]  = $this -> parking_type_model   -> get_all();
		/*********************************************************************************/
		
		$where_flag = TRUE;

		$where = '';
		//產生需求配物件的sql
		$sql_schema = $this -> pattern_lib -> demand_pair_object_sql($data ,$_start_num , $load_count);
		//$sql_schema = $sql_schema . " limit $_start_num , $load_count";
		$container=array();
		$object_modal =array();
		$market_modal =array();
		
		
		$_data = $this->base_data_lib->collcet_object_market_ajax();

		$_data["currency"]  	= $this -> base_data_lib	-> get_currency_array();

		$_data["object_state"]  = $this -> object_state_model   -> get_object_state();		
		$_data["market_state"]  = $this -> market_state_model   -> get_market_state();		
		$_data["building_name"] = $this -> building_name_model  -> get_building_name();		
		$_data["parking_type"]  = $this -> base_data_lib   -> get_parking_type_array();
			
		//print_r($sql_schema);
		
		foreach($sql_schema as $key => $_sql){
			
			//HTML要用的資料			
			$_result = $this -> pattern_model -> just_do_query($_sql);
			$html['result'] = $_result;
			
			
			
			
			if($key==0){
				//object 跳窗～～＠＠～～＝ ＝wtf～～～～～～～～～～～～～～～～～～～～～～
				
				foreach($html['result']->result() as $row){					
					$_data["object"]=$row;
					$_data["client"]=$this->client_model->get_client_info($row->client_id);
					$_data["contact"]=$this->contact->get_contact_by_id($row->client_contact_id);
					$_data["required_level"] 	= $this ->base_data_lib ->get_required_level_model_array();

					

					$object_modal[]=$this -> load -> view('demand/object_info' , $_data  , true);
				}
				
				//顯示結果object
				$container[1]=$this -> load -> view('demand/demand_pair_ajax' , $html  , true);
				
			}elseif($key==1){
				//market 跳窗～～＠＠～～＝ ＝wtf～～～～～～～～～～～～～～～～～～～～～～
				
				foreach($html['result']->result() as $row){
					
					$_data["object"]=$row;
					$_data["required_level"] 	= $this ->base_data_lib ->get_required_level_model_array();
					$_data["client"]			=$this->client_model->get_client_info($row->client_id);
					$_data["contact"]			=$this->contact->get_contact_by_id($row->client_contact_id);


					$market_modal[]				=$this -> load -> view('demand/market_info' , $_data  , true);
				}
				
				//顯示結果demand
				$container[2]=$this -> load -> view('demand/market_pair_ajax' , $html  , true);
				
			}
			
		}


		$html["rr"]=$this -> pattern_model -> just_do_query($sql_schema[0]);
		$html["page"]=$_page;
		//0－＞table 的head
		echo $container[0]=$this -> load -> view('demand/headpair/demand_pair_ajax_head' ,$html  , true);
		echo $container[1]; //1－＞table 的物件
		echo $container[2]; //2－＞table 的市調
		echo '</table>';//3－＞table 的end
		
		
		//object 跳窗～～＠＠～～＝ ＝wtf～～～～～～～～～～～～～～～～～～～～～～
		foreach($object_modal as $key => $value){
			echo $value;
		}
		
		//Market 跳窗～～＠＠～～＝ ＝wtf～～～～～～～～～～～～～～～～～～～～～～
		foreach($market_modal as $key => $value){
			echo $value;
		}
		
		//html['result'] = $_result;
		
		
		
		//$this -> load -> view('demand/demand_pair_ajax', $html,true);

	}


	//儲存結果
	public function demand_pair_object_market_result() {
		
		//print_r($_POST);
		$object=$_POST["object"]?$_POST["object"]:'';
		$market=$_POST["market"]?$_POST["market"]:'';
		$demand_id=$_POST["demand_id"]?$_POST["demand_id"]:'';
		$ymdhis_mh5=$_POST["ymdhis_mh5"]?$_POST["ymdhis_mh5"]:'';
		//ymdhis_mh5
		
		$this->pattern_model->insert_demand_pair_result($demand_id ,$object , $market,$ymdhis_mh5);
		
	}
	
	//儲存結果 market pair demand
	public function market_pair_demand_result() {
		
		$market_id  	=	$_POST["market_id"]?$_POST["market_id"]:'';
		$demand_many	=	$_POST["demand_many"]?$_POST["demand_many"]:'';
		$ymdhis_mh5	=	$_POST["ymdhis_mh5"]?$_POST["ymdhis_mh5"]:'';
		
		$data =array(	'ymdhis_mh5'=>$ymdhis_mh5 , 
						'demand'=>$demand_many,
						'market_id'=>$market_id);		
		$this->pattern_model->insert($data);
		
	}
	
	
	//儲存結果 market pair demand
	
	public function rec_object_pair_demand_result() {
		
		$object_id  	=	$_POST["object_id"]?$_POST["object_id"]:'';
		$demand_many	=	$_POST["demand_many"]?$_POST["demand_many"]:'';
		$ymdhis_mh5	=	$_POST["ymdhis_mh5"]?$_POST["ymdhis_mh5"]:'';
		
		$data =array(	'ymdhis_mh5'=>$ymdhis_mh5 , 
						'demand'=>$demand_many,
						'object_id'=>$object_id);	
							
		$this->pattern_model->insert($data);
		
	}
	
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
